package dao;

import java.sql.*;

public class DBConnectionManager {

    private String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";//���������
    private String url = "jdbc:microsoft:sqlserver://127.0.0.1:7788;DatabaseName=dbBooks";//������ݿ����Ӵ�
    private String user = "sa";//��
    private String password = "admin";//��ݿ��

    public void setDriverName(String newDriverName) {
        driverName = newDriverName;
    }

    public String getDriverName() {
        return driverName;
    }

    public void setUrl(String newUrl) {
        url = newUrl;
    }

    public String getUrl() {
        return url;
    }

    public void setUser(String newUser) {
        user = newUser;
    }

    public String getUser() {
        return user;
    }

    public void setPassword(String newPassword) {
        password = newPassword;
    }

    public String getPassword() {
        return password;
    }

    public void closeConnection(Connection dbConnection) {
        try {

            if (dbConnection != null && (!dbConnection.isClosed())) {
                dbConnection.close();
            }
        } catch (SQLException sqlEx) {
            sqlEx.printStackTrace();
        }
    }

    public void closeResultSet(ResultSet res) {
        try {
            if (res != null) {
                res.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭语句
     */
    public void closePreparedStatement(PreparedStatement pStatement) {
        try {
            if (pStatement != null) {

                pStatement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void closeStatement(Statement statement) {
        try {
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public  Connection getConnection() {
        try {
          Class.forName(driverName);
            Connection conn=DriverManager.getConnection(url, user, password);
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

